<?php

defined('SYSPATH') or die('Access error!');

class Controller_Ajax extends Controller
{

    public function __construct(Request $request)
    {
        parent::__construct($request);
        header('Content-Type', 'application/json');
        if (!Request::$is_ajax)
        {
            $this->request->redirect(URL::site());
            return;
        }
    }

    //获取子分类
    public function action_sub_area()
    {
        $id = intval($_POST['id']);
        if (empty($id))
        {
            echo '';
            exit;
        }
        $area_cache = $this->get_cache_file('area');
        $area = array();
        if (Arr::validate_arr($area_cache))
        {
            foreach ($area_cache as $value)
            {
                if ($value['parent_id'] == $id)
                {
                    $area[] = $value;
                }
            }
        }
        echo json_encode($area);
        exit;
    }

    //提问的分词操作
    public function action_splitword()
    {
        $str = Arr::get($_POST, 'word', '');
        $word = SplitWord::instance()->get_keyword(SplitWord::instance()->split_result($str));
        SplitWord::instance()->Clear();
        echo $word;
        exit;
    }

    //公用添加收藏
    public function action_add_collect()
    {
        if (!count(Session::instance()->get('user')) > 0)
        {
            echo '请先登录';
            exit;
        }
        $user_id = Session::instance()->get_detail('user', 'id');
        $id = intval(Arr::get($_POST, 'id'));
        $type = HTML::get_safe_content(Arr::get($_POST, 'type'));
        $title = HTML::get_safe_content(Arr::get($_POST, 'title', ''));
        if (empty($id) || empty($type))
        {
            echo '提交的数据不对';
            exit;
        }
        $collect = Model_Collect::factory('collect');
        if ($collect->is_exist(array('item_id' => $id, 'user_id' => $user_id, 'type' => $type)))
        {
            echo '请不要重复收藏';
            exit;
        }

        $data = array();
        $data['add_time'] = Date::get_time();
        $data['item_id'] = $id;
        $data['type'] = $type;
        $data['title'] = $title;
        $data['user_id'] = $user_id;
        $collect->insert($data);
        $temp = Statics::instance()->get_table_by_type($type);
        Statics::instance()->update_static($temp['table'], array($temp['key'] => $id), 'collects');
        echo '收藏成功';
        exit;
    }

    //根据分类的id获取某个分类的所有子分类
    public function action_get_sub_category()
    {
        $parent_id = Arr::get($_POST, 'parent_id', '');
        $type = Arr::get($_POST,'type','');
        if(empty($parent_id) || empty($type) ){
            echo 0;
            exit;
        }
        
        $sub_category = $this->get_date_as_array($parent_id,$type);
        if(empty($sub_category)){
            echo 0;
            exit;
        }
        echo json_encode($sub_category);
        exit;
    }

    //设置缓存文件
    public function set_cache_file($category_type, $data)
    {
        $category_data = Core::config('data_cache');
        return Cache::instance()->write($category_data['file'][$category_type], $data);
    }

    //获取普通数据的缓存
    public function get_cache_file($category_type)
    {
        $category_data = Core::config('data_cache');
        if (file_exists($category_data['file'][$category_type]))
        {
            return Cache::instance()->read($category_data['file'][$category_type]);
        }
    }

    //返回等级多维数组
    public function get_date_as_array($current_id, $category_type)
    {
        $data = $this->get_cache_file($category_type);
        Newtree::instance()->setTree($data, 'id', 'parent_id', 'name');
        $temp = Newtree::instance()->getArrayList($current_id);
        return $temp;
    }

    //获取分类的option
    public function get_category_cache_option($category_type, $selected_id = 0, $current_id = 0)
    {
        $category_data = Core::config('data_cache');
        $data = $this->get_cache_file($category_type);
        Newtree::instance()->setTree($data, 'id', 'parent_id', 'name');
        if ($current_id == 0)
        {
            $current_id = $category_data['id'][$category_type];
        }
        $temp = Newtree::instance()->get_tree($current_id, $selected_id);
        Newtree::instance()->delete();
        return $temp;
    }

    //获取城市的缓存文件
    public function get_area_cache_file()
    {
        $area_category = $this->get_cache_file('area');
        $area = array();
        if (Arr::validate_arr($area_category))
        {
            foreach ($area_category as $key => $value)
            {
                if ($value['id'] == SYSTEM_AREA_CATEGORY)
                {
                    continue;
                }
                if ($value['parent_id'] == SYSTEM_AREA_CATEGORY)
                {
                    $area['province'][$key] = $value;
                }
                else
                {
                    $area['city'][$key] = $value;
                }
            }
        }
        return $area;
    }

    // 获取当前位置(分类的)
    public function get_category_position($id, $type, $url)
    {
        $category_list = $this->get_cache_file($type);
        Newtree::instance()->setTree($category_list, 'id', 'parent_id', 'name');
        $position_arr = Newtree::instance()->get_pos($id);
        Newtree::instance()->delete();
        if (Arr::validate_arr($position_arr))
        {
            foreach ($position_arr as $value)
            {
                if ($value['parent_id'] == 0)
                {
                    Breadcrumbs::add($value['name'], URL::site('trade/index'));
                    continue;
                }
                Breadcrumbs::add($value['name'], URL::site($url . '/' . $value['id']));
            }
        }
    }

    //title需要的字符串（分类的名称）
    public function get_category_title($id, $type)
    {
        $category_list = $this->get_cache_file($type);
        Newtree::instance()->setTree($category_list, 'id', 'parent_id', 'name');
        $position_arr_temp = Newtree::instance()->get_pos($id);
        Newtree::instance()->delete();
        $position_arr = array_reverse($position_arr_temp);
        if (Arr::validate_arr($position_arr))
        {
            $str = '';
            foreach ($position_arr as $value)
            {
                $str .= $value['name'] . ' - ';
            }

            return rtrim(trim($str), '-');
        }
    }

    //获取分类页面的seo资料
    public function get_category_seo($id, $type)
    {
        $category_list = $this->get_cache_file($type);
        $title = $this->get_category_title($id, $type);
        if (Arr::validate_arr($category_list))
        {
            $seo_arr = array(
                'title' => $title,
                'keywords' => $category_list[$id]['keywords'],
                'description' => $category_list[$id]['description'],
            );
            $this->set_seo($seo_arr);
        }
    }

    //某个信息的详细的内容页面的seo资料
    public function detail_seo($id, $title, $keyword, $description)
    {
        //获取问答的分类id
        $category_seo = $this->get_category_title($id, 'trade');
        $title = $title . ' - ' . $category_seo;

        $seo_arr = array(
            'title' => $title,
            'keywords' => $keyword,
            'description' => $description,
        );

        $this->set_seo($seo_arr);
    }

    //根据缓存文件获取某个分类的所有子分类
    public function get_category_childs($id, $type)
    {
        $category_list = $this->get_cache_file($type);
        Newtree::instance()->setTree($category_list, 'id', 'parent_id', 'name');
        $childs_arr = Newtree::instance()->getChilds($id);
        Newtree::instance()->delete();
        return $childs_arr;
    }

}